﻿using RabbitMQ.Client;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RabbitMQTest
{
    class Program
    {
        /// <summary>
        /// 发送方
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            try
            {
                ConnectionFactory factory = new ConnectionFactory();
                factory.HostName ="localhost";
                factory.UserName = "yy";
                factory.Password = "hello!";
                using (var conn = factory.CreateConnection())
                {
                    using (var channal = conn.CreateModel())
                    {
                        //消息持久化
                        bool durable = true;
                        //创建一个hello的队列
                        channal.QueueDeclare("task_queue", durable, false,false,null);
                        //string message = "Hello World";
                        #region MyRegion
                        string message = GetMessage(args);

                        var properties = channal.CreateBasicProperties();
                        //消息持久化
                        properties.Persistent = true;
                        //properties.SetPersistent(true);
                        properties.DeliveryMode = 2;
                        #endregion

                        var body = Encoding.UTF8.GetBytes(message);
                        channal.BasicPublish("", "hello", null, body);
                        Console.WriteLine("set  {0}",message);
                        Console.ReadKey();
                    }
                }


            }
            catch (Exception e)
            {

                throw;
            }
        }

        private static string GetMessage(string[] args)
        {
            return ((args.Length > 0) ? string.Join(" ", args) : "Hello World!");
        }


    }
}
